const fp = require('lodash/fp');
// 数据
// horsepwoer 马力， dollar_value 价格， in_stock 库存

const cars = [{
    name: 'Ferrari FF',
    horsepwoer: 660,
    dollar_value: 700000,
    in_stock: true,
}, {
    name: 'Spyker C12 Zagato',
    horsepwoer: 650,
    dollar_value: 648000,
    in_stock: false,
}, {
    name: 'Jaguar XKR-S',
    horsepwoer: 550,
    dollar_value: 132000,
    in_stock: false,
},{
    name: 'Audi R8',
    horsepwoer: 525,
    dollar_value: 114200,
    in_stock: false,
}, {
    name: 'Aston Martin One-77',
    horsepwoer: 750,
    dollar_value: 1850000,
    in_stock: true,
}, {
    name: 'Pagani Huayra',
    horsepwoer: 700,
    dollar_value: 1300000,
    in_stock: false,
}];

// 练习一
// 获取最后一个元素的指定属性
const f1 = fp.flowRight(fp.prop('in_stock'), fp.last);
console.log(f1(cars));

// 练习二
// 获取第一个元素的指定属性
const f2 = fp.flowRight(fp.prop('name'), fp.first);
console.log(f2(cars));
 
// 练习三
// 重构
let _average = function(xs){
    return fp.reduce(fp.add, 0, xs)/xs.length;
}
const averageDollarValue = fp.flowRight(_average, fp.map('dollar_value'));
console.log(averageDollarValue(cars));

// 练习四
// 字符串转换 [‘Hello World’] -》[‘hello_world’]

let _underscore = fp.replace(/\W+/g, '_');
const sanitizeNames = fp.map(fp.flowRight(fp.toLower, _underscore));
console.log(sanitizeNames(['Hello World', 'Nice to meet you']));